//
//  FoodThemeListViewController.m
//  ArtOfLiving
//
//  Created by xalo on 16/5/3.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "FoodThemeListViewController.h"
#import "FoodUpdateTableViewCell.h"
#import "FoodMainModel.h"

@interface FoodThemeListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *parameter;       //数据请求参数
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;       //存放主题列表model数据

@end

@implementation FoodThemeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - --------tableView的代理方法---------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceMArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FoodMainModel *model = self.dataSourceMArr[indexPath.row];
    FoodUpdateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodUpdateCELL"];
    [cell setCellWithModel:model];
    return cell;
}


#pragma mark - --------数据解析与请求--------
- (void)fetchDataSourceWithUrlString:(NSString *)urlString parameter:(NSDictionary *)parameter {
    
    [LHDataRequestTools getFetchDataWithUrlString:urlString paramenters:parameter success:^(id data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *dataArr = [dic objectForKey:@"data"];
        for (NSDictionary *item in dataArr) {
            
            FoodMainModel *model = [[FoodMainModel alloc] init];
            [model setValuesForKeysWithDictionary:item];
            [self.dataSourceMArr addObject:model];
        }
    } fail:^{
        
        NSLog(@"食物主题列表解析错误");
    } view:self.view];
}

#pragma mark - -------懒加载----------
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 108)];
        [_tableView registerNib:[UINib nibWithNibName:@"FoodUpdateTableViewCell" bundle:nil] forCellReuseIdentifier:@"foodUpdateCELL"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableDictionary *)parameter {
    
    if (!_parameter) {
        
        _parameter = [[NSMutableDictionary alloc] initWithDictionary:@{@"start":@0,@"num":@6}];
    }
    return _parameter;
}

- (NSMutableArray *)dataSourceMArr {
    
    if (!_dataSourceMArr) {
        
        _dataSourceMArr = [[NSMutableArray alloc] init];
    }
    return _dataSourceMArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
